package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductDetailsService;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.manager.service.ProductSkuService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.service.exception.GuliException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * ClassName: ProductServiceImpl
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/6 13:49
 * @Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailsService productDetailsService;
    @Resource
    private ProductSkuService productSkuService;
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Override
    public PageInfo<Product> findByPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        // 1. 开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        // 2. 根据条件查询
        List<Product> list = productMapper.findByKeywords(productDto);
        // 3. 封装返回值
        PageInfo<Product> page = new PageInfo<>(list);
        return page;
    }

    /**
     * 商品保存
     * 涉及到的表 product  product_details  product_sku
     * @param product
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Product product) {
        // product
        product.setStatus(0);
        product.setAuditStatus(0);
        product.setAuditMessage("");
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        productMapper.save(product);
        // product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setCreateTime(new Date());
        productDetails.setUpdateTime(new Date());
        productDetailsService.save(productDetails);
        // product_sku
        int i = 0;
        for (ProductSku productSku : product.getProductSkuList()) {
            productSku.setSkuCode(product.getId() + "-" + i++);
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setProductId(product.getId());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSku.setCreateTime(new Date());
            productSku.setUpdateTime(new Date());
            productSkuService.save(productSku);
        }

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Product getById(Long id) {
        Product product = productMapper.getById(id);
        List<ProductSku> productSkus = productSkuService.getByProductId(id);
        product.setProductSkuList(productSkus);
        ProductDetails productDetails = productDetailsService.getByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    /**
     * 根据id修改
     * @param product
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Product product) {
        // 修改三张表
        // 1. product
        product.setUpdateTime(new Date());
        productMapper.updateById(product);
        // 2. product_details
        ProductDetails productDetails = productDetailsService.getByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsService.updateById(productDetails);
        // 3. product_sku
        for (ProductSku productSku : product.getProductSkuList()) {
            productSkuService.updateById(productSku);
        }

    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);
        productDetailsService.deleteByProductId(id);
        productSkuService.deleteById(id);
    }


    /**
     * 跟新审核状态
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        product.setAuditStatus(auditStatus);
        String auditMessage = "";
        switch (auditStatus){
            case 0: auditMessage = "初始值";break;
            case 1: auditMessage = "审核通过";break;
            case 2: auditMessage = "审核未通过";break;
            default:throw new GuliException(202,"审核状态出错辣！");
        }
        product.setAuditMessage(auditMessage);
        product.setUpdateTime(new Date());
        productMapper.updateById(product);
    }

    /**
     * 商品的上下架状态修改
     *      1. 如果商品已经下架了则不能修改为上架  ---> 因为商品的价格是波动的
     *      2. 商品spu下架了，则商品sku也应该下架
     * @param id
     * @param status
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long id, Integer status) {
        // 1. 修改商品spu的状态
        Product product = productMapper.getById(id);
        if(product.getStatus() == -1 && status ==1 ){
            throw new GuliException(203,"该商品已经下架了，不能重新上架了！");
        }
        product.setStatus(status);
        product.setUpdateTime(new Date());
        productMapper.updateById(product);
        // 2. 修改商品sku的状态
        productSkuService.updateStatusByProductId(product.getId(),status);

    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku  productSku = productSkuMapper.getById(skuId);
        return productSku;
    }


}
